<?php
// 本类由系统自动生成，仅供测试用途
namespace Group\Controller;
use Think\Controller;
use Common\Model\OptionModel;
class IndexController extends Controller
{
    protected $model;

    public function __construct()
    {
        parent::__construct();
        if(!session('userdata')){
            header('location:'.U('Group/Login/index'));
            exit;
        }
    }

    public function index()
    {
        $mobile=I('get.mobile');
        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);
        $this->assign('user',$user);

        //var_dump($mobile);
        if(!empty($mobile)){
            //var_dump(123);
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $searchdata=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$searchdata['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if(in_array($searchdata['id'],$memberarr) or $pan){
                $memberarr=array($searchdata['id']);
            }else{
                $this->error('对不起,没有该成员');
            }
        }

        //开始时间
        $start=I('get.start_time');
        $start=strtotime($start);
        if(empty($start)){
            $start=strtotime(date('Y-m'));
        }
        $this->assign('start',$start);

        //结束时间
        $end=I('get.end_time');
        $end=strtotime($end);
        if(empty($end)){
            $end=time();
        }
        $this->assign('end',$end);

        //所以组成员及佣金
        $where['a.id']=array('in',$memberarr);
        $where1['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $sql=M('futures_commission')->where($where1)->select(false);
        $commission=M('follow')->alias('a')->field('a.id,a.nickname,a.mobile,sum(b.commission) as commission')->where($where)->join('left join '.$sql.' b on a.id=b.commission_user_id')->order('commission desc')->group('a.id')->select();

        //获取所有组成员下的用户
        $newpepole=0;
        foreach ($memberarr as $val) {
            $level=array();
            $level['_string'] = "(route like '{$val}') or (route regexp ',{$val},') or (route regexp '^{$val},') or (route regexp ',{$val}$')";
            $level['mobile'] =array('neq',"");
            $follow=M('follow')->field('id')->where($level)->select();
            $level['subscribe_time']=array(array('egt',$start),array('elt',$end+86400));
            $newfollow=M('follow')->where($level)->count();
            $newpepole+=$newfollow;
            foreach($follow as $value){
                $inarr1[]=$value['id'];
            }
        }
        session('newpeople',$newpepole);

        //获取所有用户的充值记录
        $inarr=array_merge($memberarr,$inarr1);
        $rechg['user_id']=array('in',array_unique($inarr));
        $rechg['is_pay']=1;
        $rechg['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $rechargedata=M('futures_recharge')->field('sum(money) as money')->where($rechg)->find();


        //获取所有用户的提现记录
        $drawals['user_id']=array('in',array_unique($inarr));
        $drawals['is_pay']=1;
        $drawals['pay_time']=array(array('egt',$start),array('elt',$end+86400));
        $drawalsdata=M('futures_withdrawals')->field('sum(money) as money')->where($drawals)->find();


        //获取所有用户的佣金
        $coms['commission_user_id']=array('in',$inarr);
        $coms['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $commissiondata=M('futures_commission')->field('sum(commission) as money')->where($coms)->group('status')->select();

        //获取恒指订单信息
        if(!empty($inarr)) {
        $henmodel=new OptionModel('order');
//        $a=$henmodel->select();
//        var_dump($a);
//        exit;

            $wherehen['user_id'] = array('in', $inarr1);
            $wherehen['status'] = 2;
//        $wherehen['start_time']=array(array('egt',$start),array('elt',$end));
            $wherehen['start_time'] = array('$gte' => $start, '$lte' => $end + 86400);
            $hendata = $henmodel->where($wherehen)->field('invest,profit')->select();

            $henorderprice = 0;
            foreach ($hendata as $v) {
                $henorderprice += $v['invest'];
            }
            $showhendata = array($henorderprice);


            //获取欧元订单信息
            $euromodel = new OptionModel('euro_order');

            $eurodata = $euromodel->field('invest,profit')->where($wherehen)->select();
            $euroorderprice = 0;
            foreach ($eurodata as $v) {
                $euroorderprice += $v['invest'];
            }
            $showeurodata = array($euroorderprice);

            //获取伦敦金订单信息
            $goldmodel = new OptionModel('goldorder');
            $golddata = $goldmodel->field('invest,profit')->where($wherehen)->select();
            $goldorderprice = 0;
            foreach ($golddata as $v) {
                $goldorderprice += $v['invest'];
            }
            $showgolddata = array($goldorderprice);


            //获取美元订单信息
            $dollarmodel = new OptionModel('dollarorder');
            $dollardata = $dollarmodel->field('invest,profit')->where($wherehen)->select();
            $dollarorderprice = 0;
            foreach ($dollardata as $v) {
                $dollarorderprice += $v['invest'];
            }
            $showdollardata = array($dollarorderprice);

            $this->assign('showdollardata',$showdollardata);
            $this->assign('showhendata',$showhendata);
            $this->assign('showeurodata',$showeurodata);
            $this->assign('showgolddata',$showgolddata);
        }


        $this->assign('commission',$commission);
        $this->assign('rechargedata',$rechargedata);
        $this->assign('drawalsdata',$drawalsdata);
        $this->assign('commissiondata',$commissiondata);

        $this->display();
    }

    public function userinfo(){
        $mobile=I('get.mobile');
        $start=I('get.start_time');
        $start=strtotime($start);
        if(empty($start)){
            $start=strtotime('2015-1-1');
        }
        $this->assign('start',$start);

        //结束时间
        $end=I('get.end_time');
        $end=strtotime($end);
        if(empty($end)){
            $end=time();
        }
        $this->assign('end',$end);
        $level['subscribe_time']=array(array('egt',$start),array('elt',$end+86400));

            $user = session('userdata');
            $memberarr = unserialize($user['g_member']);
            $this->assign('user',$user);

            $level['mobile'] =array('neq',"");

            if($mobile){
                $ser['mobile']=$mobile;
                $ser['nickname']=$mobile;
                $ser['_logic']='or';
                $user=M('follow')->field('id,route')->where($ser)->find();
                $roudata=explode(',',$user['route']);
                foreach($roudata as $val){
                    if(in_array($val,$memberarr)){
                        $pan=true;
                        break;
                    }
                }
                if(in_array($user['id'],$memberarr)){
                    $slevel['_string'] = "(route like '{$user['id']}') or (route regexp ',{$user['id']},') or (route regexp '^{$user['id']},') or (route regexp ',{$user['id']}$')";
                    $sdata=M('follow')->where($slevel)->select();
                    foreach($sdata as $vk){
                        $smemberarr[]=$vk['id'];
                        $memberarr[]=$user['id'];
                        $level['id']=array('in',$smemberarr);
                    }

                }elseif($pan){
                    $level['id']=$user['id'];
                }else{
                    $this->error('对不起,没有该成员');
                }
            }else{
                foreach ($memberarr as $val) {
                    $level['_string'] .= "(route like '{$val}') or (route regexp ',{$val},') or (route regexp '^{$val},') or (route regexp ',{$val}$') or";
                }
                $level['_string']=trim($level['_string'],'or');
            }

            $count=M('follow')->where($level)->count();
            $page=new \Think\Page($count,20);
            $follow=M('follow')->field('id,nickname,mobile,money,subscribe_time,route')->where($level)->limit($page->firstRow.','.$page->listRows)->order('money desc')->group('id')->select();
        foreach($follow as $key=>$value){
            $pop="";
            $pop=explode(',',$value['route']);
            $count=count($pop);
            for($i=0;$i<$count;$i++){
                $pid=array_pop($pop);
                if(in_array($pid,$memberarr)){
                    $pdata=M('follow')->field('nickname')->find($pid);
                    $follow[$key]['shangji']=$pdata['nickname'];
                    continue;
                }
            }
        }
        $showdata=$follow;
        $this->assign('page',$page);
        $this->assign('showdata',$showdata);
        $this->display();
    }

    public function new_user(){
        $mobile=I('get.mobile');

        $start=I('get.start_time');
        $start=strtotime($start);
        if(empty($start)){
            $start=strtotime(date('Y-m'));
        }
        $this->assign('start',$start);

        //结束时间
        $end=I('get.end_time');
        $end=strtotime($end);
        if(empty($end)){
            $end=time();
        }
        $this->assign('end',$end);
        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);

        if(empty($mobile)){
            $level['subscribe_time']=array(array('egt',$start),array('elt',$end+86400));
            foreach ($memberarr as $val) {
                $level['_string'] .= "(route like '{$val}') or (route regexp ',{$val},') or (route regexp '^{$val},') or (route regexp ',{$val}$') or";
            }
            $level['_string']=trim($level['_string'],'or');
        }else{
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $user=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$user['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if(in_array($user['id'],$memberarr)){
                $level['subscribe_time']=array(array('egt',$start),array('elt',$end+86400));
                $level['_string'] = "(route like '{$user['id']}') or (route regexp ',{$user['id']},') or (route regexp '^{$user['id']},') or (route regexp ',{$user['id']}$') ";
            }elseif($pan){
                $level['id']=$user['id'];
            }else{
                $this->error('对不起,没有该成员');
            }
        }

        $level['mobile'] = array('neq',"");
        $count=M('follow')->where($level)->count();
        $page=new \Think\Page($count,20);
        $follow=M('follow')->field('id,nickname,mobile,money,subscribe_time,route')->where($level)->order('subscribe_time desc')->limit($page->firstRow.','.$page->listRows)->group('id')->select();

        foreach($follow as $key=>$value){
            $pop="";
            $pop=explode(',',$value['route']);
            $count=count($pop);
            for($i=0;$i<$count;$i++){
                $pid=array_pop($pop);
                if(in_array($pid,$memberarr)){
                    $pdata=M('follow')->field('nickname')->find($pid);
                    $follow[$key]['shangji']=$pdata['nickname'];
                    continue;
                }
            }
        }

        $pai['subscribe_time']=array(array('egt',$start),array('elt',$end+86400));
        $pai['mobile']=array('neq',"");
        foreach($memberarr as $vv){
            $pai['_string'] .= "((route like '{$vv}') or (route regexp ',{$vv},') or (route regexp '^{$vv},') or (route regexp ',{$vv}$')) or";
        }
        $pai['_string']=trim($pai['_string'],'or');
        $pshow=M('follow')->field('id,nickname,mobile,money,subscribe_time,route')->where($pai)->group('id')->select();
        $count=0;
        foreach($memberarr as $key=>$val){
            $fdata=M('follow')->find($val);
            $memdata[$fdata['id']]=$fdata['nickname'];
        }

        $paidata=array();
        foreach($pshow as $key=>$vk){
            $pop=explode(',',$vk['route']);
            $count=count($pop);
            for($i=0;$i<$count;$i++){
                $pid=array_pop($pop);
                if(in_array($pid,$memberarr)){
                    $paidata[$memdata[$pid]]+=1;
                    break;
                }
            }
        }
        arsort($paidata);
        $this->assign('paidata',$paidata);
        $showdata=$follow;
        $this->assign('showdata',$showdata);
        $this->assign('page',$page);
        $this->display();
    }
}